package com.my.kq.dao;

import com.my.kq.entity.*;
import com.my.kq.uitl.BaseDAO;


import java.util.List;

public class EmployeeDao extends BaseDAO {
    public Employee dlu(String employeeNumber, String password) throws Exception {
        String sql="select * from employee where employee_number=? and password=? ";
        Object[]obj={employeeNumber,password};
        Employee one = getOne(sql, new Employee(), obj);
        return one;
    }

    public Employee Empcx(Integer id) throws Exception {
        String sql="select * from employee where id=?";
        Object[]obj={id};
        Employee one = getOne(sql, new Employee(), obj);
        return one;
    }

    public Employee getEmployee(int eNum) throws Exception {
        String sql="select * from employee where employee_number=?";
        Object[]obj={eNum};
        Employee one = getOne(sql, new Employee(), obj);
        return one;
    }

    public int empUpdate(Integer id, String employeeNumber, String name, String password, String gender, String date, String telephone, String email, String address, String education,String s,String notes) throws Exception {
        String sql="update employee set employee_number=?,name=?,gender=?,birthday=?,telephone=?,email=?,address=?,education=?,in_time=?,notes=?,password=? where id=?";
        Object[]obj={employeeNumber,name,gender,date,telephone,email,address,education,s,notes,password,id};
        int i = addOrupdOrdel(sql, obj);
        return i;
    }



    //分页查询数据   参数为pageNo
    public List<EmployeeListDTO> getPageList(int pageNo) throws Exception {
        String sql = "select e.id," +
                "e.employee_number," +
                "e.name," +
                "e.gender," +
                "e.telephone," +
                "d.name depName," +
                "p.name posName," +
                "e.in_time " +
                " from employee e " +
                " left join department d on e.department_number =d.departmentNumber " +
                " left join position p on e.position_number =p.positionNumber limit ?,?;";
        Object[] obj = {(pageNo - 1) * 10, 10};
        List<EmployeeListDTO> list = getList2(sql, new EmployeeListDTO(), obj);
        return list;
    }

    //总记录条数
    public int getTotal() throws Exception {
        String sql = "select count(1) from employee";
        return getCount(sql, new Object[0]);
    }

    //单个查看
    public EmployeeQueryDTO getEmployeeById(int employee_number) throws Exception {
        String sql = "select e.employee_number," +
                "                e.name," +
                "                e.gender," +
                "                e.birthday," +
                "                e.telephone," +
                "                e.email," +
                "                e.address," +
                "               e.education," +
                "               d.name as depName," +
                "                p.name as posName," +
                "                e.in_time," +
                "               e.notes " +
                "                 from employee e " +
                "               left join department d on e.department_number =d.departmentNumber \n" +
                "               left join position p on e.position_number =p.positionNumber \n" +
                "                where e.employee_number =? ;";
        Object[] obj = {employee_number};
        EmployeeQueryDTO one = getOne2(sql, new EmployeeQueryDTO(), obj);
        return one;
    }

    //删除员工
    public void EmployeeDelById(String employee_number) throws Exception {
        String sql = "delete from employee where employee_number=?;";
        Object[] obj = {employee_number};
        addOrupdOrdel(sql, obj);
    }

    //添加员工
    public void addEmploy(Employee employee) throws Exception {
        String sql = "insert into employee values(null,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] obj = {employee.getEmployee_number(), employee.getName(), employee.getGender(), employee.getBirthday(), employee.getTelephone(),
                employee.getEmail(), employee.getAddress(), null, employee.getEducation(), employee.getDepartment_number(), employee.getPosition_number(),
                null, employee.getPassword(), employee.getNotes()};
        addOrupdOrdel(sql, obj);
    }

    //查询最大工号值
    public Integer gonghao() throws Exception {
        String sql = "select * from employee  order by employee_number desc;";
        List<Employee> list = getList(sql, new Employee(), new Object[0]);
        return list.get(0).getEmployee_number();
    }

    //查询部门名称
    public List<Department> selectBuMen() throws Exception {
        String sql = "select * from department;";
        List<Department> list = getList2(sql, new Department(), new Object[0]);
        return list;
    }

    //查询职称名称
    public List<Position> selectZhiCheng() throws Exception {
        String sql = "select * from position;";
        List<Position> list = getList2(sql, new Position(), new Object[0]);
        return list;
    }

    //预修改
    public Employee yuXiuGai(String employee_number) throws Exception {
        String sql = "select * from employee where employee_number=?;";
        Object[] obj = {employee_number};
        Employee one = getOne(sql, new Employee(), obj);
        return one;
    }

    //修改员工信息
    public void updateYuanGong(String employeeNumber, String name, String password, String gender, String brithday, String telephone, String email, String address, String education, String positionNumber, String notes) throws Exception {
        String sql = "update employee set name=?,password=?,gender=?,birthday=?,telephone=?,email=?,address=?,education=?,position_number=?,notes=? where employee_number=?";
        Object[] obj = {name, password, gender, brithday, telephone, email, address, education, positionNumber, notes, employeeNumber};
        addOrupdOrdel(sql, obj);
    }

}
